 package cn.itcast.context;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//用类装载器读取资源文件
public class ServletContextDemo7 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		test3();
	}
	
	
	//文件太大，只能用servletContext     classes 这个程序指的是src的目录下 
	public void test4() throws IOException{
		//读取PranavMistry_2009I_480.mp4,并拷贝到e:\根目录下
		
		//path=c:\asdf\adsd\add\PranavMistry_2009I_480.mp4
		//path=PranavMistry_2009I_480.mp4
		////获取文件名
		String path = this.getServletContext().getRealPath("/WEB-INF/classes/mysql-8.0.12-winx64.zip");
		String filename = path.substring(path.lastIndexOf("\\")+1);
		
		
		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/mysql-8.0.12-winx64.zip");
		byte buffer[] = new byte[1024];
		int len = 0;
		
		FileOutputStream out = new FileOutputStream("D:\\" + filename);
		while((len=in.read(buffer))>0){
			out.write(buffer,0,len);
		}
		
		out.close();
		in.close();
	}
	
	
	//通过类装载器读取资源文件的注意事项:不适合装载大文件，否则会导致jvm内存溢出
	public void test3(){
		InputStream in = ServletContextDemo7.class.getClassLoader().getResourceAsStream("mysql-8.0.12-winx64.zip");
		System.out.println(in);
	}
	
	//读取类路径下面、包下面的资源文件
	public void test2() throws IOException{
		InputStream in = ServletContextDemo7.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties");
		System.out.println(in);
		Properties prop = new Properties();
	     prop.load(in);
		
		String driver = prop.getProperty("driver");
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(driver);
	}

	/***
	 *  * 测试不通
	 * @throws IOException
	 */
	private void test1() throws IOException {
		//获取到装载当前类的类装载器  
		ClassLoader loader = ServletContextDemo7.class.getClassLoader();
		
		//用类装载器装载db.properties文件
		InputStream in = loader.getResourceAsStream("db1.properties");
		Properties prop = new Properties();
		prop.load(in);
		
		String driver = prop.getProperty("driver");
		String url = prop.getProperty("url");
		String username = prop.getProperty("username");
		String password = prop.getProperty("password");
		
		System.out.println(driver);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
